package com.jz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jz.mapper.auto.TWordsMapper;
import com.jz.module.dto.TWordsDto;
import com.jz.module.dto.base.PageInfoDto;
import com.jz.module.entity.TWords;
import com.jz.module.entity.TWordsExample;
import com.jz.service.WordsService;
import com.jz.util.ResultUtil;
import com.jz.util.SysCode;
import com.jz.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WordsServiceImpl implements WordsService {

    @Autowired
    @SuppressWarnings("all")
    private TWordsMapper tWordsMapper;

    @Override
    public ResultUtil page(TWordsDto dto) {
        PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        TWordsExample example = new TWordsExample();
        if (StringUtils.isNotBlank(dto.getHostId())) {
            example.createCriteria().andHostIdEqualTo(dto.getHostId());
        }
        if (StringUtils.isNotBlank(dto.getMessageId())) {
            example.createCriteria().andMessageIdEqualTo(dto.getMessageId());
        }
        example.setOrderByClause("create_time desc");
        List<TWords> tWordss = tWordsMapper.selectByExample(example);
        PageInfo<TWords> info = new PageInfo<>(tWordss);
        PageInfoDto<TWordsDto> result = new PageInfoDto<TWordsDto>();
        BeanUtils.copyProperties(info, result);
        return ResultUtil.success(result);
    }


    @Override
    public ResultUtil list(TWordsDto dto) {
        TWordsExample example = new TWordsExample();
        if (StringUtils.isNotBlank(dto.getHostId())) {
            example.createCriteria().andHostIdEqualTo(dto.getHostId());
        }
        if (StringUtils.isNotBlank(dto.getMessageId())) {
            example.createCriteria().andMessageIdEqualTo(dto.getMessageId());
        }
        example.setOrderByClause("create_time desc");
        List<TWords> tWordss = tWordsMapper.selectByExample(example);
        return ResultUtil.success(tWordss);
    }

    @Override
    public ResultUtil query(TWordsDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getId())) {
            return ResultUtil.paramError();
        }
        TWords tWords = tWordsMapper.selectByPrimaryKey(dto.getId());
        if (tWords == null) {
            return ResultUtil.error(SysCode.NO_DATA);
        }
        TWordsDto result = new TWordsDto();
        BeanUtils.copyProperties(tWords, result);
        return ResultUtil.success(result);
    }

    @Override
    public ResultUtil save(TWordsDto dto) {
        if (dto == null || StringUtils.isAnyBlank(
                dto.getHostId(),
                dto.getHostName(),
                dto.getHostType(),
                dto.getContent()
        )) {
            return ResultUtil.paramError();
        }

        // insert
        TWords insert = new TWords();
        BeanUtils.copyProperties(dto, insert);

        insert.setId(UUIDUtil.getUUID());
        insert.setCreateTime(new Date());
        int res = tWordsMapper.insert(insert);
        if (res < 1) {
            return ResultUtil.error(SysCode.INSERT_ERROR);
        }

        return ResultUtil.success();
    }

    @Override
    public ResultUtil delete(TWordsDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getId())) {
            return ResultUtil.paramError();
        }
        int i = tWordsMapper.deleteByPrimaryKey(dto.getId());
        if (i > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.error(SysCode.DELETE_ERROR);
    }
}
